import React, { useState, useEffect } from 'react';
import { connect } from 'dva';
import EditTemplate from '@/components/EditTemplate';
import CapitalSource from '@/pages/components/capitalSource';
import { timeFormat } from '@/utils/common';
import moment from 'moment';
const BuildEdit = ({ location, dispatch, uuid, update }: any) => {
  const [detail, setDetail] = useState(null);
  const { id, readOnly } = location.query;

  const saveUrl = id ? '/api/check/v1/update' : '/api/check/v1/create';
  useEffect(() => {
    if (id) {
      if (update === false) return;

      dispatch({
        type: '$request/post',
        payload: {
          url: '/api/check/v1/detail',
          data: {
            uuid: uuid || id,
            page: 0,
            size: 10,
          },
        },
      }).then(res => {
        console.log('储备项目申报详情：', res);
        res.createTime = timeFormat(res.project.createTime);
        (res.date = readOnly
          ? timeFormat(res.date, 'YYYY-MM-DD')
          : moment(res.date)),
          setDetail(res);
      });
    }
  }, []);
  const projectChange = (uuid, data) => {
    if (update === false) return;
    dispatch({
      type: '$request/post',
      payload: {
        url: '/api/check/v1/project/query',
        data: { uuid },
      },
    }).then(list => {
      const res = list.find(item => item.uuid == uuid);
      res.createTime = timeFormat(new Date(), 'YYYY-MM-DD');
      res.projectUuid = res.uuid;
      setDetail(res);
    });
  };
  const filters = [
    {
      title: '基础信息',
      list: [
        {
          key: ['project', 'name'],
          type: 'select',
          label: '项目名称',
          asyncopt: {
            url: '/api/check/v1/project/query',
          },
          prop: {
            onChange: (uuid, data) => {
              console.log('uuid', uuid);
              projectChange(uuid, data);
            },
          },
          span: 12,
        },
        {
          key: id ? ['project', 'code'] : 'code',
          type: 'text',
          label: '项目代码',
          placeholder: '请输入姓名',
          span: 12,
        },
        {
          key: id ? 'org' : ['ownerOrgUnit', 'name'],
          type: 'text',
          label: '业主单位',
          span: 12,
        },
        {
          key: 'createTime',
          type: 'text',
          label: '登记日期',
          span: 12,
        },
        {
          key: 'date',
          type: 'datepicker',
          label: '验收日期',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'warranty',
          type: 'input',
          label: '质保期',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'org',
          type: 'textarea',
          label: '验收单位',
          span: 12,
        },
        {
          key: 'expert',
          type: 'textarea',
          label: '验收专家',
          span: 12,
        },
        {
          key: 'reportList',
          type: 'upload',
          label: '验收报告',
          rules: [{ required: true }],
          span: 24,
        },
        {
          key: 'planList',
          type: 'upload',
          label: '工作计划',
          span: 24,
        },
        {
          key: 'dataList',
          type: 'upload',
          label: '资料清单',
          span: 24,
        },
        {
          key: 'suggestionList',
          type: 'upload',
          label: '验收意见',
          span: 24,
        },
        {
          key: 'questionList',
          type: 'upload',
          label: '整改问题',
          span: 24,
        },
      ],
    },
  ];
  const preproccess = data => {
    const reportUuidList = data.reportList
      ? data.reportList.map(item => item.uuid)
      : [];
    const planUuidList = data.planList
      ? data.planList.map(item => item.uuid)
      : [];
    const dataUuidList = data.dataList
      ? data.dataList.map(item => item.uuid)
      : [];
    const suggestionUuidList = data.suggestionList
      ? data.suggestionList.map(item => item.uuid)
      : [];
    const questionUuidList = data.questionList
      ? data.questionList.map(item => item.uuid)
      : [];
    return {
      reportUuidList,
      planUuidList,
      dataUuidList,
      suggestionUuidList,
      questionUuidList,
      ...data,
    };
  };
  return (
    <div>
      <EditTemplate
        filters={filters}
        initialValues={detail}
        updateUrl={saveUrl}
        preproccess={preproccess}
        readOnly={readOnly}
      />
    </div>
  );
};

export default connect((state: any) => state.auth)(BuildEdit);
